package com.exam.controller.exam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.exam.entity.UserPaper;
import com.exam.entity.Users;
import com.exam.service.TestPaperService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.exam.entity.TestPaper;
import com.exam.entity.vo.QuestionBankVo;
import com.exam.service.ExamService;

@Controller
@RequestMapping(value = "/exam")
public class ExamController {

    @Autowired
    private ExamService examService;

    @Autowired
    private TestPaperService testPaperService;

    /**
     * 用户查看全部试卷
     *
     * @return
     */
    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public ModelAndView index() {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        List<TestPaper> allTestPaper = examService.findAllTestPaper();
        for (int i = 0; i < allTestPaper.size(); i++) {
            TestPaper testPaper = allTestPaper.get(i);
            //遍历试卷时间，设置试卷状态
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date start = simpleDateFormat.parse(testPaper.getStartDate());
                Date end = simpleDateFormat.parse(testPaper.getEndDate());
                //如果在考试时间，设置为1
                if (date.after(start) && date.before(end)){
                	testPaper.setIsStart(1);
				}else{
					testPaper.setIsStart(0);
				}

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        modelAndView.addObject("allTestPaper", allTestPaper);
        modelAndView.setViewName("_exam/my_exam");
        return modelAndView;
    }

    /**
     * 跳转到试卷页面，如果还没考试，则可以点击开始考试
     * 如果已经考过了，则可以查看试卷
     *
     * @return
     */
    @RequestMapping(value = "/exam-{id}.html", method = RequestMethod.GET)
    public ModelAndView exam(@PathVariable String id, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testPaterId", id);
        Users myUser = (Users)session.getAttribute("myUser");
        boolean b = testPaperService.checkScore(myUser.getUserId(), Integer.parseInt(id));
        if (b==false){
            modelAndView.setViewName("/_exam/message");
            return modelAndView;
        }
        examService.findJudgmentQuestionAndChoiceQuestion(modelAndView, id, session);
		
/*		List<TestPaper> allTestPaper = examService.findAllTestPaper();
		modelAndView.addObject("allTestPaper", allTestPaper);
*/

        return modelAndView;
    }


    /**
     * 提交试卷
     * @param questionBankVos 题目列表
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/submitpapers", method = RequestMethod.POST)
    public List<QuestionBankVo> submitPapers(@RequestBody List<QuestionBankVo> questionBankVos, HttpSession session) {

        List<QuestionBankVo> judgmentSystem = examService.JudgmentSystem(questionBankVos, session);

        return judgmentSystem;
    }

    /**
     * 试卷模板映射
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/autoGenerate", method = RequestMethod.POST)
    public boolean autoGenerate(HttpSession session, TestPaper testPaper) {

        boolean autoGenerate = examService.autoGenerate(session, testPaper.getTestpaperId());

        return autoGenerate;
    }


    /**
     * 保存用户答题情况
     * @param session
     * @param userPaper
     */
    @PostMapping("/saveUserPaper")
    public void saveUserPaper(HttpSession session, UserPaper userPaper) {
        Users user = (Users) session.getAttribute("myUser");
        userPaper.setUserId(user.getUserId());

        System.out.println(userPaper.getTestPaperId() + "testPaterId****");
        testPaperService.add(userPaper);
    }


    /**
     * 跳转到用户要查询的试卷页面
     * 根据用户id和试卷id查询用户的试卷答题情况
     */
    @GetMapping("/toUserPaper")
    public ModelAndView toUserPaper(UserPaper userPaper) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/_exam/user_paper");

        return modelAndView;
    }
}

